//
//  SerialTableViewCell.m
//  XPhoto
//
//  Created by sentinel on 2020/3/31.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "SerialTableViewCell.h"
#import <UIImageView+AFNetworking.h>
#import <BKKit/NSDate+x.h>
#define CELL_RIGHT_MARGIN   10
@implementation SerialTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.addrButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.addrButton.imageEdgeInsets = UIEdgeInsetsMake(0, -1, 0, 0);
    self.addrButton.titleEdgeInsets = UIEdgeInsetsMake(0, 1, 0, 0);
    self.addrButton.contentEdgeInsets = UIEdgeInsetsMake(1, 3, 1, 3);
    
    // Initialization code
}
- (CGRect)titleLabelFrameWithPostSerial:(PostSerial *)postSerial offsetY:(CGFloat)offsetY cellWidth:(CGFloat)cellWidth{
    CGRect titleFrame = self.titleLabel.frame;
    titleFrame.origin.y = offsetY;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineBreakMode:NSLineBreakByWordWrapping];
    NSDictionary *titleAttributes = @{
        NSFontAttributeName: self.titleLabel.font,
        NSParagraphStyleAttributeName: style
        
    };
    titleFrame.size = [postSerial.title boundingRectWithSize:CGSizeMake(cellWidth - self.titleLabel.frame.origin.x - CELL_RIGHT_MARGIN, CGFLOAT_MAX)
                                                     options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                                  attributes:titleAttributes
                                                     context:nil].size;
    return titleFrame;
}
- (CGRect)addrButtonFrameWithPostSerial:(PostSerial *)postSerial offsetY:(CGFloat)offsetY cellWidth:(CGFloat)cellWidth{
    CGRect addrButtonFrame = self.addrButton.frame;
    addrButtonFrame.origin.y = offsetY;
    CGFloat maxWidth = cellWidth - addrButtonFrame.origin.x  - CELL_RIGHT_MARGIN - self.addrButton.contentEdgeInsets.left - self.addrButton.contentEdgeInsets.right - 3 - addrButtonFrame.size.height-30;
    CGSize addrButtonTitleSize = [postSerial.addr boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
                                                               options:NSStringDrawingUsesLineFragmentOrigin
                                                            attributes:@{NSFontAttributeName: self.addrButton.titleLabel.font}
                                                               context:nil].size;
    addrButtonFrame.size.width = addrButtonTitleSize.width + self.addrButton.contentEdgeInsets.left + self.addrButton.contentEdgeInsets.right + 3 + addrButtonFrame.size.height;
    return addrButtonFrame;
}
- (CGRect)descLabelFrameWithPostSerial:(PostSerial *)postSerial offsetY:(CGFloat)offsetY cellWidth:(CGFloat)cellWidth{
    CGRect descLabelFrame = self.descLabel.frame;
    descLabelFrame.origin.y = offsetY;
    CGSize descLabelSize = [[self descTextWithPostSerial:postSerial] boundingRectWithSize:CGSizeMake(cellWidth - descLabelFrame.origin.x - CELL_RIGHT_MARGIN, CGFLOAT_MAX)
                                                                                       options:NSStringDrawingUsesLineFragmentOrigin
                                                                                    attributes:@{NSFontAttributeName:self.descLabel.font}
                                                                                       context:nil].size;
    descLabelFrame.size.height = descLabelSize.height;
    return descLabelFrame;;
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat vPadding = 6;
    CGFloat offsetY = self.titleLabel.frame.origin.y;
    CGRect titleFrame = [self titleLabelFrameWithPostSerial:self.postSerial offsetY:offsetY cellWidth:self.bounds.size.width];
    self.titleLabel.frame = titleFrame;
    offsetY += titleFrame.size.height + vPadding;
    
    CGRect addrButtonFrame = [self addrButtonFrameWithPostSerial:self.postSerial offsetY:offsetY cellWidth:self.bounds.size.width];
    if (self.postSerial.addr) {
        self.addrButton.frame = addrButtonFrame;
        offsetY += addrButtonFrame.size.height + vPadding;
    }
    CGRect descLabelFrame = [self descLabelFrameWithPostSerial:self.postSerial offsetY:offsetY cellWidth:self.bounds.size.width];
    self.descLabel.frame = descLabelFrame;
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (NSString *)descTextWithPostSerial:(PostSerial *)postSerial{
    NSString *descText = [NSString stringWithFormat:NSLocalizedString(@"serial_desc_format", @"系列"), [postSerial.createTime format:DATE_FORMAT_PATTERN_DATE], postSerial.postCount];
    return descText;
}
- (CGFloat) descLabelHeightWithPostSerial:(PostSerial *)postSerial width:(CGFloat)width{
    NSString *descText = [self descTextWithPostSerial:postSerial];
    CGRect descBounds = [descText boundingRectWithSize:CGSizeMake(width - (self.bounds.size.width - self.descLabel.bounds.size.width), CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: self.descLabel.font} context:nil];
    return descBounds.size.height;
}
- (void) setPostSerial:(PostSerial *)postSerial{
    _postSerial = postSerial;
    [self.coverImageView setImageWithURL:[NSURL URLWithString:postSerial.cover]];
    self.titleLabel.text = postSerial.title;
    self.descLabel.text = [self descTextWithPostSerial:postSerial];
    
    [self.addrButton setTitle:postSerial.addr forState:UIControlStateNormal];
    if(postSerial.addr){
        self.addrButton.hidden = NO;
    }
    if (!postSerial.addr) {
        self.addrButton.hidden = YES;
    }
    [self setNeedsLayout];
}
- (CGFloat)heightWithPostSerial:(PostSerial *)postSerial width:(CGFloat)width{
    CGFloat vPadding = 6;
    CGFloat h = self.titleLabel.frame.origin.y;
    CGRect titleFrame = [self titleLabelFrameWithPostSerial:postSerial offsetY:h cellWidth:width];
    h += titleFrame.size.height + vPadding;

    CGRect addrButtonFrame = [self addrButtonFrameWithPostSerial:postSerial offsetY:h cellWidth:width];
    if (postSerial.addr) {
        h += addrButtonFrame.size.height + vPadding;
    }
    CGRect descLabelFrame = [self descLabelFrameWithPostSerial:postSerial offsetY:h cellWidth:width];
    h += descLabelFrame.size.height + self.titleLabel.frame.origin.y;
    h = MAX (h, self.coverImageView.frame.origin.y*2 + self.coverImageView.frame.size.height);
    return h;
}

@end
